package 国际象棋0710;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class GameListener implements Data, ActionListener, MouseListener, ListSelectionListener {
    Tool tool = new Tool();
    Graphics g ;
    String action;
    Chess[][] chessList = tool.initChessList();
    Chess[][] pad = new Chess[ROW][COL];
    Boolean isGaming = false;
    JRadioButton jrb1 ;
    JRadioButton jrb2 ;
    Chess isChoose = null;
    int preR = EMPTY, preC = EMPTY, nowR = EMPTY, nowC = EMPTY;
    int actNum = 1;
    int playerFlag = 0;


    @Override
    public void actionPerformed(ActionEvent e) {
        String ac = e.getActionCommand();
        action = ac;
        switch (ac){
            case "开始游戏" -> {
                isGaming = true;
                tool.drawChessPad(g);
                tool.initChesses(chessList, g);
                for (int i = 0; i < 8; i++) {
                    for (int j = 0; j < 8; j++) {
                        if (chessList[i][j] != null){
                            System.out.print(chessList[i][j].kind);
                        }
                    }
                    System.out.println();
                }
                jrb1.setEnabled(!isGaming);
                jrb2.setEnabled(!isGaming);
                JButton button = (JButton) e.getSource();
                button.setText("结束游戏");
            }
            case "结束游戏" ->{
                isGaming = false;
                actNum = 1;
                jrb1.setEnabled(true);
                jrb2.setEnabled(true);
                JButton button = (JButton) e.getSource();
                button.setText("开始游戏");
            }

        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int x1 = e.getX();
        int y1 = e.getY();
        int r = tool.calChessROW(y1);
        int c = tool.calChessCOL(x1);
        if (action == null || action.equals("结束游戏")){
            return;
        }
        System.out.println("r:" + r + " c:" + c);
        if (action.equals("开始游戏")){
            if (actNum % 2 == 1 && chessList[r][c].chessFlag == 0){
                return;
            }else if (actNum % 2 == 0 && chessList[r][c].chessFlag == chessList[preR][preC].chessFlag){
                return;
            }
            playerFlag = 1;
            if (isChoose == null){
                preR = r;
                preC = c;
                actNum++;
                isChoose = chessList[r][c];
                System.out.println("已选中：" + chessList[r][c].kind);
            }else {
                nowR = r;
                nowC = c;
                //是否能走到这格
                if (!tool.isFall( preR,  preC,  nowR,  nowC, chessList)){
                    return;
                }
                tool.move( preR,  preC,  nowR,  nowC, chessList, g);
                chessList[preR][preC].chessFlag = 0;
                chessList[preR][preC].kind = "空";
                System.out.print("行动:  ");
                System.out.println(preR + "," + preC + " -> " + r + "," + c);
                actNum++;
                isChoose = null;
                //输出棋盘
                for (int i = 0; i < 8; i++) {
                    for (int j = 0; j < 8; j++) {
                        if (chessList[i][j] != null){
                            System.out.print(chessList[i][j].kind);
                        }
                    }
                    System.out.println();
                }
            }
            System.out.println("------------------------");
//            tool.drawChessList(chessList, g);

        }


    }

    @Override
    public void mousePressed(MouseEvent e) {
        int x1 = e.getX();
        int y1 = e.getY();

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        int x2 = e.getX();
        int y2 = e.getY();
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
